package com.uenpay.im.ui.old.activity.red;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.aswife.ui.RoundedImageView;
import com.qyx.android.message.type.PacketType;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.contacts.UserDetailActivity;
import com.uenpay.im.ui.old.activity.personal.MeDetailActivity;
import com.uenpay.im.configuration.APIConfiguration;

import java.text.MessageFormat;
import java.util.ArrayList;

/**
 * 私聊红包列表
 *
 * @author SL
 *
 */
public class RedDetailPersonalAdapter extends BaseAdapter {

	private LayoutInflater inflater = null;
	private ArrayList<RedRecordEntity> entities = new ArrayList<RedRecordEntity>();
	private Context mContext;

	public RedDetailPersonalAdapter(Context _Context,
			ArrayList<RedRecordEntity> _entities) {
		entities = _entities;
		mContext = _Context;
		inflater = LayoutInflater.from(_Context);
	}

	@Override
	public int getCount() {
		return entities.size();
	}

	@Override
	public Object getItem(int arg0) {
		return entities.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		RedDetailViewHodler hodler = null;
		final RedRecordEntity redEntity = entities.get(arg0);
		if (arg1 == null) {
			hodler = new RedDetailViewHodler();
			arg1 = inflater.inflate(R.layout.item_red_personal_record_layout,
					arg2, false);
			hodler.name = (TextView) arg1.findViewById(R.id.name_tv);
			hodler.date = (TextView) arg1.findViewById(R.id.date_tv);
			hodler.money = (TextView) arg1.findViewById(R.id.money_tv);
			hodler.imageView = (RoundedImageView) arg1
					.findViewById(R.id.avatar);
			arg1.setTag(hodler);
		} else {
			hodler = (RedDetailViewHodler) arg1.getTag();
		}
		hodler.name.setText(redEntity.nickName);

		hodler.imageView.SetUrl(APIConfiguration.getAvatarUrlNormal(
				redEntity.custId, PacketType.PERSONAL));

		if (TextUtils.isEmpty(redEntity.recordTime)) {
			hodler.money.setText(MessageFormat.format(mContext.getResources()
					.getString(R.string.take_apart), redEntity.recordMoney));
			hodler.date.setVisibility(View.GONE);
		} else {
			hodler.money.setText(MessageFormat.format(mContext.getResources()
					.getString(R.string.red_size), redEntity.recordMoney));
			hodler.date.setText(redEntity.recordTime);
			hodler.date.setVisibility(View.VISIBLE);
		}

		hodler.imageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (!TextUtils.isEmpty(redEntity.custId)) {

					Intent intent = new Intent();
					if (redEntity.custId.equals(IMApplication.getCustId())) {
						intent.setClass(mContext, MeDetailActivity.class);
						mContext.startActivity(intent);
					} else {
						intent.setClass(mContext, UserDetailActivity.class);
						intent.putExtra("cust_id", redEntity.custId);
						mContext.startActivity(intent);
					}

				}
			}
		});
		return arg1;
	}

	private class RedDetailViewHodler {
		private TextView name, date, money;
		private RoundedImageView imageView;
	}

}
